package com.lineage.server.timecontroller.server;

import com.lineage.server.datatables.lock.CharMapTimeReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.S_PacketBoxGame;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TimeMap extends TimerTask {
  private static final Log _log = LogFactory.getLog(TimeMap.class);
  
  String mapName;
  
  int teleportmapid;
  
  int locy;
  
  int locx;
  
  int maptime;
  
  int id;
  
  int mapid;
  
  CopyOnWriteArrayList<L1PcInstance> pclist = new CopyOnWriteArrayList<>();
  
  public int getId() {
    return this.id;
  }
  
  public void setId(int id) {
    this.id = id;
  }
  
  public String getMapName() {
    return this.mapName;
  }
  
  public void setMapName(String mapName) {
    this.mapName = mapName;
  }
  
  public int getMapid() {
    return this.mapid;
  }
  
  public void setMapid(int mapid) {
    this.mapid = mapid;
  }
  
  public int getMaptime() {
    return this.maptime;
  }
  
  public void setMaptime(int maptime) {
    this.maptime = maptime;
  }
  
  public int getLocx() {
    return this.locx;
  }
  
  public void setLocx(int locx) {
    this.locx = locx;
  }
  
  public int getLocy() {
    return this.locy;
  }
  
  public void setLocy(int locy) {
    this.locy = locy;
  }
  
  public int getTeleportmapid() {
    return this.teleportmapid;
  }
  
  public void setTeleportmapid(int teleportmapid) {
    this.teleportmapid = teleportmapid;
  }
  
  public void run() {
    try {
      Iterator<L1PcInstance> iterator = this.pclist.iterator();
      while (iterator.hasNext()) {
        L1PcInstance _pc = iterator.next();
        if (_pc.isDead())
          return; 
        short map = _pc.getMapId();
        if (map >= 4001 && map <= 4050)
          map = 4001; 
        if (map == this.mapid) {
          if (_pc.isTimeMap()) {
            int time = _pc.getMapTime(map);
            if (time >= this.maptime) {
              _pc.stopTimeMap();
              showTime(_pc, 2);
              deluser(_pc);
              _pc.sendPackets((ServerBasePacket)new S_SystemMessage("你已经达到此地图的限制时间"));
              teleport(_pc);
              return;
            } 
            _pc.updateMapTime(1);
            showTime(_pc, 1);
            continue;
          } 
          _pc.setTimeMap(true);
          continue;
        } 
        showTime(_pc, 2);
        _pc.stopTimeMap();
        deluser(_pc);
      } 
    } catch (Throwable e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void showTime(L1PcInstance _pc, int b) {
    if (this.mapid == 4001)
      return; 
    if (b == 1) {
      int time = _pc.getMapTime(_pc.getMapId());
      _pc.sendPackets((ServerBasePacket)new S_PacketBox(153, this.maptime - time, null));
    } else if (b == 2) {
      _pc.sendPackets((ServerBasePacket)new S_PacketBoxGame(72));
    } 
  }
  
  private void teleport(L1PcInstance _pc) {
    L1Teleport.teleport(_pc, this.locx, this.locy, (short)this.teleportmapid, 5, true);
  }
  
  public void adduser(L1PcInstance _pc) {
    Iterator<L1PcInstance> iterator = this.pclist.iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      if (pc.getId() == _pc.getId())
        return; 
    } 
    this.pclist.add(_pc);
  }
  
  private void deluser(L1PcInstance _pc) {
    int i = 0;
    while (i < this.pclist.size()) {
      if (((L1PcInstance)this.pclist.get(i)).getId() == _pc.getId()) {
        if (this.mapid != 4001)
          CharMapTimeReading.get().update(_pc.getId(), this.mapid, _pc.getMapTime(this.mapid)); 
        this.pclist.remove(i);
      } 
      i++;
    } 
  }
}
